﻿//-------------------------------------
// WBFSSync - WBFSSync.exe
// Log.cs
// Copyright 2009 Caian (ÔmΣga Frøst) <frost.omega@hotmail.com>
//
// WBFSSync is Licensed under the terms of the Microsoft Reciprocal License (Ms-RL)
//-------------------------------------

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

namespace WBFSSync
{
    internal static class ResizeImage
    {
        public static Image Resize(Image original, int width, int height)
        {
            // Create a new blank canvas.  The resized image will be drawn on this canvas.
            Bitmap bmPhoto = new Bitmap(width, height, original.PixelFormat);
            bmPhoto.SetResolution(72, 72);

            Graphics grPhoto = Graphics.FromImage(bmPhoto);
            grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
            grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
            grPhoto.PixelOffsetMode = PixelOffsetMode.HighQuality;

            grPhoto.DrawImage(original, new Rectangle(0, 0, width, height), 0, 0, original.Width, original.Height, GraphicsUnit.Pixel);

            return (Image)bmPhoto;
        }

        public static Image LoadAndResize(string source, int width, int height)
        {
            Image original = Image.FromFile(source);

            // Create a new blank canvas.  The resized image will be drawn on this canvas.
            Bitmap bmPhoto = new Bitmap(width, height, original.PixelFormat);
            bmPhoto.SetResolution(72, 72);

            Graphics grPhoto = Graphics.FromImage(bmPhoto);
            grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
            grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
            grPhoto.PixelOffsetMode = PixelOffsetMode.HighQuality;

            grPhoto.DrawImage(original, new Rectangle(0, 0, width, height), 0, 0, original.Width, original.Height, GraphicsUnit.Pixel);
            original.Dispose();

            return (Image)bmPhoto;
        }

        public static int ResizeFile(string source, string dest, int width, int height)
        {
            Image original = null;
            Bitmap bmPhoto = null;
            Graphics grPhoto = null;

            try
            {
                original = Image.FromFile(source);
                if ((original.Width == width) && (original.Height == height))
                {
                    original.Save(dest);
                    original.Dispose();
                    original = null;
                    return 0;
                }

                // Create a new blank canvas.  The resized image will be drawn on this canvas.
                bmPhoto = new Bitmap(width, height, original.PixelFormat);
                bmPhoto.SetResolution(72, 72);

                grPhoto = Graphics.FromImage(bmPhoto);
                grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
                grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
                grPhoto.PixelOffsetMode = PixelOffsetMode.HighQuality;

                grPhoto.DrawImage(original, new Rectangle(0, 0, width, height), 0, 0, original.Width, original.Height, GraphicsUnit.Pixel);

                bmPhoto.Save(dest, ImageFormat.Png);

                grPhoto.Dispose();
                bmPhoto.Dispose();
                original.Dispose();
            }
            catch
            {
            }

            grPhoto = null;
            bmPhoto = null;
            original = null;

            return 0;
        }

        public static int CropAndResizeFile(string source, string dest, int width, int height,
            int crop_x, int crop_y, int crop_w, int crop_h)
        {
            Image original = Image.FromFile(source);

            // Create a new blank canvas.  The resized image will be drawn on this canvas.
            Bitmap bmPhoto = new Bitmap(width, height, original.PixelFormat);
            bmPhoto.SetResolution(72, 72);

            Graphics grPhoto = Graphics.FromImage(bmPhoto);
            grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
            grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
            grPhoto.PixelOffsetMode = PixelOffsetMode.HighQuality;

            grPhoto.DrawImage(original, new Rectangle(0, 0, width, height), crop_x, crop_y, crop_w, crop_h, 
                GraphicsUnit.Pixel);

            bmPhoto.Save(dest);

            grPhoto.Dispose();
            bmPhoto.Dispose();
            original.Dispose();

            grPhoto = null;
            bmPhoto = null;
            original = null;

            return 0;
        }
    }
}
